//
//  ARTDeviceListCell.m
//  ChartsDemo
//
//  Created by yunda on 2022/1/18.
//

#import "ARTDeviceListCell.h"

@implementation ARTDeviceListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    J_ViewRadius(_viewbg, 10);
    
    _nameLabel.textColor = J_ColorFromHEX(0x222222);
    _nameLabel.font = J_Font(16);
    _macLabel.numberOfLines = 0;
    _stateLabel.textColor = J_ColorFromHEX(0x0DA462);
    _stateLabel.font = J_Font(12);
    
    J_ViewRadius(_setButton, 11);
    J_ViewBorder(_setButton, J_ColorFromHEX(0x0095B6), 1);
    [_setButton setTitle:@"设置>" forState:UIControlStateNormal];
    [_setButton setTitleColor:J_ColorFromHEX(0x0095B6) forState:UIControlStateNormal];
    _setButton.titleLabel.font = J_Font(12);
    _setButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    [_setButton addTarget:self action:@selector(settingAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)settingAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(deviceListCell:didClickedSetting:)]) {
        [self.delegate deviceListCell:self didClickedSetting:_dataDic];
    }
}

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"ARTDeviceListCell";
    ARTDeviceListCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *name = [dataDic j_stringForKey:@"name"];
    NSString *mac = [dataDic j_stringForKey:@"mac"];
    ARTDeviceType type = [Tool deviceTypeWithName:name];
    _setButton.hidden = YES;
    if (type == ARTDeviceTypeOximeter) {
        _logoImageVIew.image = J_ImageNamed(@"dev_oxmeter");
        _nameLabel.text = FYLocalizedString(@"oxygen_name", @"");
    }else if (type == ARTDeviceTypeFetalHeart){
        _logoImageVIew.image = J_ImageNamed(@"dev_heart");
        _nameLabel.text = FYLocalizedString(@"fhr_name", @"");
    }else if (type == ARTDeviceTypeWatch){
        _logoImageVIew.image = J_ImageNamed(FYLocalizedString(@"dev_watch", @""));
        _nameLabel.text = FYLocalizedString(@"sleep_watch_name", @"");
        _setButton.hidden = NO;
    }else if (type == ARTDeviceTypePlateau){
        _logoImageVIew.image = J_ImageNamed(@"dev_pla");
        _nameLabel.text = FYLocalizedString(@"Highland Pulse Oximeter", @"");;
    }else if(type == ARTDeviceTypePelvicMuscles){
        _logoImageVIew.image = [UIImage imageNamed:@"art_pelvic_floor_muscles_chanpintu"];
        _nameLabel.text = FYLocalizedString(@"pelvic_floor_name_", @"");
    }
    if (_setButton.hidden) {
        _macLabelRight.constant = -90;
    }else{
        _macLabelRight.constant = 10;
    }
    [self layoutIfNeeded];
    NSMutableAttributedString *nameAttribute = [[NSMutableAttributedString alloc]initWithString:name attributes:@{NSForegroundColorAttributeName:J_ColorFromHEX(0x666666),NSFontAttributeName:J_Font(12)}];
    [nameAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"(%@)",mac] attributes:@{NSForegroundColorAttributeName:J_ColorFromHEX(0x666666),NSFontAttributeName:J_Font(10)}]];
    _macLabel.attributedText = nameAttribute;
    
    NSString *defaultMac = [NSUserDefaults jk_stringForKey:ART_Storage_DefaultDeviceUUID];
    if ([mac isEqualToString:defaultMac] && ARTSingleShare.currentPeripheral.state == CBPeripheralStateConnected) {
        _stateLabel.text = FYLocalizedString(@"bluetooth_connected_n", @"");
        _stateLabel.textColor = J_ColorFromHEX(0x0DA462);
    }else{
        _stateLabel.text = FYLocalizedString(@"bluetooth_not_connected_n", @"");
        _stateLabel.textColor = J_ColorFromHEX(0x999999);
    }
    _stateLabel.adjustsFontSizeToFitWidth = YES;
    [_setButton setTitle:FYLocalizedString(@"app__setting", @"") forState:UIControlStateNormal];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
